<template>
   <div class="app-container">
      <!-- <el-divider content-position="left">停服役操作</el-divider> -->
      <el-button type="primary" @click="submit">保存</el-button>
      <el-table v-loading="loading" :data="xztList">
         <el-table-column label="编号" prop="id" width="120" />
         <el-table-column label="设备名称" prop="componentName" min-width="220" />
         <el-table-column label="设备编码" prop="componentNumber" width="220" />
         <el-table-column label="停役时间" align="center" prop="stoptime" width="220">
            <template #default="scopes">
               <el-date-picker required @change="getXd(scopes.row)" v-model="scopes.row.stoptime" style="width: 100%;"
                  type="datetime"></el-date-picker>
            </template>
         </el-table-column>
         <el-table-column label="复役时间" align="center" prop="resumetime" width="220">
            <template #default="scopes">
               <el-date-picker required v-model="scopes.row.resumetime" style="width: 100%;"
                  type="datetime"></el-date-picker>
            </template>
         </el-table-column>

         <el-table-column label="操作" align="center" prop="" width="220">
            <template #default="scopes">
               <el-button type="danger" plain icon="Delete" :disabled="multiple"
                  @click="handleDelete(scopes.row)">删除</el-button>
            </template>
         </el-table-column>
      </el-table>


      <el-divider content-position="left">设备查询</el-divider>
      <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
         <el-form-item label="设备查询" prop="componentNumber">
            <el-input v-model="queryParams.componentNumber" placeholder="请输入设备编码" clearable style="width: 240px"
               @keyup.enter="handleQuery" />
         </el-form-item>
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
         </el-form-item>
      </el-form>

      <!-- 表格数据 -->
      <el-table v-loading="loading" :data="componentList" @selection-change="handleSelectionChange">
         <el-table-column type="selection" width="55" align="center" />
         <el-table-column label="编号" prop="id" width="220" />
         <el-table-column label="设备名称" prop="componentName" min-width="420" />
         <el-table-column label="设备编码" prop="componentNumber" width="420" />
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize" @pagination="getList" />
   </div>

   <!-- 基本事件 -->
   <el-dialog :title="title" v-model="openDataScope" width="800px" append-to-body>
      <el-table v-loading="loading" :data="xdEventList" @selection-change="xdEventListSelectionChange">
         <el-table-column type="selection" width="55" align="center" />
         <el-table-column label="编号" prop="id" width="120" />
         <el-table-column label="基本事件名称" prop="eventName" min-width="220" />
         <el-table-column label="基本事件描述" prop="eventNumber" width="220" />
      </el-table>
      <template #footer>
         <div class="dialog-footer">
            <el-button type="primary" @click="submitDataScope">确 定</el-button>
            <el-button @click="cancelDataScope">取 消</el-button>
         </div>
      </template>
   </el-dialog>
</template>

<script setup name="Component">
import { listComponent, editComponent } from "@/api/factoryInfo/component/component";
const { proxy } = getCurrentInstance();
import { ref } from 'vue';

const openDataScope = ref(false);
const eventList = ref([]); //选择基本事件总集合
const xdEventList = ref([]); //选中设备 下面的 基本事件集合
const oneEventList = ref([]); //单独设备选中集合
const componentList = ref([]);
const xztList = ref([]);
const total = ref(0);
const data = reactive({
   form: {},
   queryParams: {
      pageNum: 1,
      pageSize: 5
   },
});
const { queryParams, form, rules } = toRefs(data);

/** 查询列表 */
function getList() {
   listComponent(queryParams.value).then(response => {
      componentList.value = response.rows;
      total.value = response.total;
   });
}

getList();

/** 搜索按钮操作 */
function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
   debugger
   xztList.value = selection;
}

/**删除选定 */
function handleDelete(item) {
   debugger
   xztList.value.forEach(function (element, index, arr) {
      debugger
      if (element.id == item.id) {
         xztList.value.splice(index, 1);
      }
   })
}

function getXd(row) {
   debugger
   openDataScope.value = true;
   xdEventList.value = row.eventsList;
}

/** 取消按钮（数据权限）*/
function cancelDataScope() {
   openDataScope.value = false;
   reset();
}

/** 基本事件多选框选中数据 */
function xdEventListSelectionChange(selection) {
   oneEventList.value = selection;
}

function submitDataScope() {
   eventList.value.push(oneEventList.value);
   openDataScope.value = false;
}

async function submit() {
   xztList.value.forEach(function (element, index, arr) {
      element.eventsList = [];
   })

   xztList.value.forEach(function (element, index, arr) {
      xdEventList.value.forEach(function (e, index, arr) {
         if (element.id == e.componentId) {
            element.eventsList.push(e);
         }
      })
   })


   proxy.$modal.confirm('是否要提交设备停复役').then(function () {
      debugger
      return editComponent(xztList.value);
   }).then(() => {
      getList();
      eventList.value = []; //选择基本事件总集合
      xdEventList.value = []; //选中设备 下面的 基本事件集合
      oneEventList.value = []; //单独设备选中集合
      proxy.$modal.msgSuccess("提交成功");
   }).catch(() => { });

}
</script>